package com.example.file.service.Impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.example.file.entity.Image;
import com.example.file.entity.SysFileInfo;
import com.example.file.mapper.ImageMapper;
import com.example.file.mapper.SysFileInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.example.file.service.SysFileInfoService;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

@Service
public class SysFileInfoServiceImpl implements SysFileInfoService {
    @Value("${myFilePath}")
    private String myFilePath;
    @Value("${imageServiceUrl}")
    private String imageServiceUrl;
    @Value("${ipaddress}")
    private String ipaddress;
    @Autowired
    SysFileInfoMapper sysFileInfoMapper;
    @Autowired
    ImageMapper imageMapper;

    @Override
    public Map<String, Object> imageUpload(MultipartFile file, HttpServletRequest request) {

        List<SysFileInfo> results = new ArrayList<>();
        Map<String, Object> respMap = new HashMap<>(2);
        if (file.isEmpty()) {
            System.out.println("文件为空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        //本地路径
        //String filePath = "D:"+ File.separator+"java"+File.separator+"apache-tomcat-8.5.61-windows-x64"+File.separator+"apache-tomcat-8.5.61"+File.separator+"webapps"+File.separator+"java"+File.separator; // 上传后的路径
        //上传图片路径
        //服务器nginx上传路径
        String filePath = myFilePath;
        // 新文件名
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = fileName;
        System.out.println(filename);
        // 计算文件大小kb
        long fileSizeKb = Convert.toLong(NumberUtil.div(new BigDecimal(file.getSize()), BigDecimal.valueOf(1024)).setScale(0, BigDecimal.ROUND_HALF_UP));
        //计算文件大小信息
        String fileSizeInfo = FileUtil.readableFileSize(file.getSize());
        //图片路径
        String url = imageServiceUrl + filename;
        SysFileInfo sysFileInfo = new SysFileInfo();
        Image image = new Image();
        image.setUrl(ipaddress);
        imageMapper.insert(image);
        sysFileInfo.setFileSizeKb(fileSizeKb);
        sysFileInfo.setFileSizeInfo(fileSizeInfo);
        sysFileInfo.setFileOriginName(fileName);
        sysFileInfo.setFilePath(url);
        sysFileInfo.setFileSuffix(suffixName);
        //文件存储位置（1:阿里云，2:腾讯云，3:minio，4:本地）
        sysFileInfo.setFileLocation(4);
        sysFileInfo.setFileObjectName(filename);
        results.add(sysFileInfo);
        //完整路径
        String fullpath = ipaddress + imageServiceUrl + fileName;
        sysFileInfoMapper.insert(sysFileInfo);
        respMap.put("results", results);
        respMap.put("fullpath", fullpath);
        return respMap;
    }
}


